package cn.wuyu.baselibrary.widget;

import android.content.Context;
import android.view.View;
import android.widget.DatePicker;

import androidx.appcompat.app.AlertDialog;
import cn.wuyu.baselibrary.R;

//import android.widget.DatePicker;

/**
 * author : xuzongmeng
 * date   : 2018/3/20
 * desc   : 日期选择
 */
public class DatePickerDialog {

//    public interface OnDateSetListener{
//
//    }
//    public DatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
//        this(context, 0, callBack, year, monthOfYear, dayOfMonth);
//    }
//    public DatePickerDialog(@NonNull Context context) {
//        this(context, 0, null, Calendar.getInstance(), -1, -1, -1);
//    }
//
//    /**
//     * Creates a new date picker dialog for the current date.
//
//     */
//    public DatePickerDialog(@NonNull Context context, @StyleRes int themeResId) {
//        this(context, themeResId, null, Calendar.getInstance(), -1, -1, -1);
//    }
//    public DatePickerDialog(@NonNull Context context, @StyleRes int themeResId,
//                            @Nullable OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
//        this(context, themeResId, listener, null, year, monthOfYear, dayOfMonth);
//    }

    //    public static void showDatePickerDialog(Activity activity, final TextView tv, Calendar calendar) {
//        // Calendar 需要这样来得到
//        // Calendar calendar = Calendar.getInstance();
//        // 直接创建一个DatePickerDialog对话框实例，并将它显示出来
//        new DatePickerDialog(activity,
//                // 绑定监听器(How the parent is notified that the date is set.)
//                new android.app.DatePickerDialog.OnDateSetListener() {
//                    @Override
//                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
//                        // 此处得到选择的时间，可以进行你想要的操作
//                        tv.setText("您选择了：" + year + "年" + month
//                                + "月" + dayOfMonth + "日");
//                    }
//                }
//                // 设置初始日期
//                , calendar.get(Calendar.YEAR)
//                , calendar.get(Calendar.MONTH)
//                , calendar.get(Calendar.DAY_OF_MONTH));
//    }
    private static int year = 2000;
    private static int monthOfYear = 1;
    private static int dayOfMonth = 1;

    public static void showDatePickerDialog(Context context, int startYear, int startMonthOfYear, int startDayOfMonth, OnDateSetListener listener) {


        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setPositiveButton("设置", (dialog, which) -> {
//                if (date.length() > 0) { //清除上次记录的日期
//                    date.delete(0, date.length());
//                }
//                tvDate.setText(date.append(String.valueOf(year)).append("年").append(String.valueOf(month)).append("月").append(day).append("日"));
            listener.onDateChange(year, monthOfYear, dayOfMonth);
            dialog.dismiss();
        });
        builder.setNegativeButton("取消", (dialog, which) -> dialog.dismiss());
        final AlertDialog dialog = builder.create();
        View dialogView = View.inflate(context, R.layout.dialog_date, null);
        final DatePicker datePicker = dialogView.findViewById(R.id.datePicker);
        datePicker.init(startYear, startMonthOfYear, startDayOfMonth, (view, year1, monthOfYear1, dayOfMonth1) -> {
            year = year1;
            monthOfYear = monthOfYear1;
            dayOfMonth = dayOfMonth1;
        });
        dialog.setTitle("生日选择");
        dialog.setView(dialogView);
        dialog.show();
    }

    public interface OnDateSetListener {
        void onDateChange(int year, int monthOfYear, int dayOfMonth);
    }
}
